﻿#region Copyright and license information
// Copyright 2001-2009 Stephen Colebourne
// Copyright 2009-2012 Jon Skeet
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;

namespace NodaTime.TimeZones
{
    /// <summary>
    ///   An <see cref="IDateTimeZoneSource" /> for ITU (International Telecommunication Union) UTC.
    /// </summary>
    /// <remarks>
    ///   A time zone ID has the form "UTC" or "UTC+/-Offset".
    /// </remarks>
    /// <threadsafety>This type maintains no state, and all members are thread-safe. See the thread safety section of the user guide for more information.</threadsafety>
    public sealed class ItuDateTimeZoneSource : IDateTimeZoneSource
    {
        /// <summary>
        ///   The ITU (International Telecommunication Union) is responsible for UTC.
        /// </summary>
        internal const string ProviderId = "itu";

        /// <summary>
        ///   Returns the IDs of all system time zones.
        /// </summary>
        public IEnumerable<string> GetIds()
        {
            return new string[] { "UTC" };
        }

        /// <summary>
        ///   The unique identifier for this source,  "itu".
        /// </summary>
        public string Id { get { return ProviderId; } }

        /// <summary>
        /// Returns version information corresponding to the version of the assembly
        /// containing <see cref="TimeZoneInfo"/>.
        /// </summary>
        public string VersionId
        {
            get { return "ITU/UTC: 1"; }
        }

        /// <summary>
        ///   Gets the <see cref="DateTimeZone"/> for the specified ID.
        /// </summary>
        /// <remarks>
        ///   Throws because the DateTimeZoneCached intercepts all calls for UTC.
        /// </remarks>        
        DateTimeZone IDateTimeZoneSource.ForId(string id)
        {
            throw new ArgumentException(id + " is not an ITU time zone ID", "id");
        }

        /// <summary>
        ///   Maps the BCL ID to "our" ID as an identity projection.
        /// </summary>
        /// <remarks>
        ///   The return ID is in the form "UTC+/-Offset".
        /// </remarks>        
        public string MapTimeZoneId(TimeZoneInfo timeZone)
        {
            var offset = Offset.FromTimeSpan(timeZone.BaseUtcOffset);
            return new FixedDateTimeZone(offset).Id;
        }
    }
}
